package com.phoenix.sgs.faq.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class DBHelper {
	public DBHelper(Context context) {
		this.context = context;
	}

	private Context context; 
    private String rootDirectory = "/data/data/sgsfaq/data/";
    private final String DATABASE_PATH = 
    	android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/sgsfaq";
    private final String DATABASE_FILENAME = "sgs.db";

    //复制小于1M的数据库程序        
    public SQLiteDatabase openDatabase() {   
    	try {   
    		// 获得dictionary.db文件的绝对路径   
    		String databaseFilename = DATABASE_PATH+ "/" + DATABASE_FILENAME;   
    		File dir = new File(DATABASE_PATH);
    		
    		// 如果/sdcard/dictionary目录不中存在，创建这个目录   
    		if (!dir.exists())   
    			dir.mkdir();   
    		
    		if ((new File(databaseFilename)).exists())
    			(new File(databaseFilename)).delete();
    		
    		// 如果在/sdcard/dictionary目录中不存在   
    		// Library.s3db文件，则从res\raw目录中复制这个文件到   
    		// SD卡的目录（/sdcard/dictionary）   
    		if (!(new File(databaseFilename)).exists()) {   
    			InputStream is = null;
    			FileOutputStream fos = null;
    			
    			try {
					// 获得封装dictionary.db文件的InputStream对象   
					is = context.getAssets().open(DATABASE_FILENAME); //.getResources().openRawResource(R.raw.crazynovel);   
					fos = new FileOutputStream(databaseFilename);   
					byte[] buffer = new byte[1024];   
					int count = 0;   
					// 开始复制dictionary.db文件   
					while ((count = is.read(buffer)) > 0)   
					{   
					    fos.write(buffer, 0, count);   
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					if (fos != null) {
						fos.flush();
		                fos.close(); 
					}
					
					if (is != null) {
						is.close(); 
					}
				}
    		}
    		
    		// 打开/sdcard/dictionary目录中的dictionary.db文件   
    		File dbFile = new File(databaseFilename);
            SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(dbFile, null);   
            return database;  
    	} catch (Exception e) { 
    		Toast.makeText(
    				this.context, 
                	e.getMessage(),
                    Toast.LENGTH_SHORT)
                    .show();
    		e.printStackTrace();
    		return null;
    	}   
    }  
    	
    //复制文件
    private void copydb(int dbint,String databaseFilename,int FileSize) {
    	try {
    		// 获得封装dictionary.db文件的InputStream对象   
            InputStream is = context.getResources().openRawResource(dbint);   
            FileOutputStream fos = new FileOutputStream(databaseFilename);   
            byte[] buffer = new byte[FileSize];   
            int count = 0;
            
            // 开始复制dictionary.db文件   
            while ((count = is.read(buffer)) > 0) {   
                fos.write(buffer, 0, count);   
            }   
            fos.close();   
            is.close();
        } catch(Exception e) {
        }
    }

    //合并并拷贝数据
    private void CreateFromRawDbFiles(File[] filelist,FileOutputStream Fos) {
    	try {
    		for (File file : filelist) {
    			InputStream inputFile = new FileInputStream(file);
    			int TotalLength = 0;
    			try {
    				TotalLength = inputFile.available();
    			} catch (IOException e) {
    			}
   
    			// Reading and writing the file Method 1 :
    			byte[] buffer = new byte[TotalLength];
    			int len = 0;
    			try {
    				len = inputFile.read(buffer);
    			} catch (IOException e) {
    			}
    			
    			Fos.write(buffer,0,len);
    			inputFile.close();
    		}
    		Fos.close();
        } catch(IOException e) {}
    }
}
